<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%_
const tsKeyId = primaryKey.tsSampleValues[0];
const allRelationshipsByEntityNeedingOptions = Object
  .values(relationshipsByOtherEntity)
  .map(relationships => relationships.filter(rel => rel.persistableRelationship && !rel.otherEntity.embedded))
  .filter(relationships => relationships.length > 0);
const testEntityPrimaryKey0 = tsPrimaryKeySamples[0];
const testEntityPrimaryKey1 = tsPrimaryKeySamples[1];
_%>
<%_ if (clientTestFrameworkVitest) { _%>
import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest';
<%_ } _%>
import { provideHttpClient, HttpResponse } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';
<%_ if (enableTranslation) { _%>
import { TranslateModule } from '@ngx-translate/core';
<%_ } _%>
import { of, Subject, from } from 'rxjs';

import { <%= entityAngularName %>FormService } from './<%= entityFileName %>-form.service';
import { <%= entityAngularName %>Service } from '../service/<%= entityFileName %>.service';
import { I<%= entityAngularName %> } from '../<%= entityFileName %>.model';
<%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions.filter(relationships => relationships[0].otherEntity.entityAngularName !== entityAngularName)) { _%>
  <%_ const otherEntity = relationshipsByEntityNeedingOptions[0].otherEntity; _%>
import { I<%= otherEntity.entityAngularName %> } from 'app/entities/<%= otherEntity.entityFolderName %>/<%= otherEntity.entityFileName %>.model';
import { <%= otherEntity.entityAngularName %>Service } from 'app/entities/<%= otherEntity.entityFolderName %>/service/<%= otherEntity.entityFileName %>.service';
<%_ } _%>

import { <%= entityAngularName %>Update } from './<%= entityFileName %>-update';

describe('<%= entityAngularName %> Management Update Component', () => {
    let comp: <%= entityAngularName %>Update;
    let fixture: ComponentFixture<<%= entityAngularName %>Update>;
    let activatedRoute: ActivatedRoute;
    let <%= entityInstance %>FormService: <%= entityAngularName %>FormService;
    let <%= entityInstance %>Service: <%= entityAngularName %>Service;
<%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions.filter(relationships => relationships[0].otherEntity.entityAngularName !== entityAngularName)) { _%>
  <%_ const otherEntity = relationshipsByEntityNeedingOptions[0].otherEntity; _%>
    let <%= otherEntity.entityInstance %>Service: <%= otherEntity.entityAngularName %>Service;
<%_ } _%>

    beforeEach(() => {
        TestBed.configureTestingModule({
            <%_ if (enableTranslation) { _%>imports: [TranslateModule.forRoot()],<%_ } _%>
            providers: [
              provideHttpClient(),
              provideHttpClientTesting(),
              {
                provide: ActivatedRoute,
                useValue: {
                  params: from([{}]),
                },
              },
            ]
        });

        fixture = TestBed.createComponent(<%= entityAngularName %>Update);
        activatedRoute = TestBed.inject(ActivatedRoute);
        <%= entityInstance %>FormService = TestBed.inject(<%= entityAngularName %>FormService);
        <%= entityInstance %>Service = TestBed.inject(<%= entityAngularName %>Service);
<%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions.filter(relationships => relationships[0].otherEntity.entityAngularName !== entityAngularName)) { _%>
  <%_ const otherEntity = relationshipsByEntityNeedingOptions[0].otherEntity; _%>
        <%= otherEntity.entityInstance %>Service = TestBed.inject(<%= otherEntity.entityAngularName %>Service);
<%_ } _%>

        comp = fixture.componentInstance;
    });

    describe('ngOnInit', () => {
<%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions) { _%>
  <%_ const relationshipsWithCustomUniqueOptions = relationshipsByEntityNeedingOptions.filter(rel => rel.relationshipOneToOne && rel.otherRelationship); _%>
  <%_ const relationshipsWithCustomSharedOptions = relationshipsByEntityNeedingOptions.filter(rel => !relationshipsWithCustomUniqueOptions.includes(rel)); _%>
  <%_ if (relationshipsWithCustomSharedOptions.length > 0) { _%>
    <%_ const otherEntity = relationshipsByEntityNeedingOptions[0].otherEntity; _%>
        it('should call <%= otherEntity.entityAngularName %> query and add missing value', () => {
                const <%= entityInstance %> : I<%= entityAngularName %> = <%- testEntityPrimaryKey1 %>;
    <%_ for (const relationship of relationshipsWithCustomSharedOptions) { _%>
      <%_ if (relationship.collection) { _%>
                const <%= relationship.propertyName %> : I<%= otherEntity.entityAngularName %>[] = [<%- otherEntity.tsPrimaryKeySamples[0] %>];
      <%_ } else { _%>
                const <%= relationship.propertyName %> : I<%= otherEntity.entityAngularName %> = <%- otherEntity.tsPrimaryKeySamples[0] %>;
      <%_ } _%>
                <%= entityInstance %>.<%= relationship.propertyName %> = <%= relationship.propertyName %>;
    <%_ } _%>

                const <%= otherEntity.entityInstance %>Collection: I<%= otherEntity.entityAngularName %>[] = [<%- otherEntity.tsPrimaryKeySamples[0] %>];
                <%= clientTestFramework %>.spyOn(<%= otherEntity.entityInstance %>Service, 'query').mockReturnValue(of(new HttpResponse({ body: <%= otherEntity.entityInstance %>Collection })));
                const additional<%= otherEntity.entityAngularNamePlural %> = [
    <%_ for (const relationship of relationshipsWithCustomSharedOptions) { _%>
                    <% if (relationship.collection) { %>...<% } %><%= relationship.propertyName %>,
    <%_ } _%>
                ];
                const expectedCollection: I<%= otherEntity.entityAngularName %>[] = [...additional<%= otherEntity.entityAngularNamePlural %>, ...<%= otherEntity.entityInstance %>Collection];
                <%= clientTestFramework %>.spyOn(<%= otherEntity.entityInstance %>Service, 'add<%= otherEntity.entityAngularName %>ToCollectionIfMissing').mockReturnValue(expectedCollection);

                activatedRoute.data = of({ <%= entityInstance %> });
                comp.ngOnInit();

                expect(<%= otherEntity.entityInstance %>Service.query).toHaveBeenCalled();
                expect(<%= otherEntity.entityInstance %>Service.add<%= otherEntity.entityAngularName %>ToCollectionIfMissing).toHaveBeenCalledWith(
                  <%= otherEntity.entityInstance %>Collection,
                  ...additional<%= otherEntity.entityAngularNamePlural %>.map(i => expect.objectContaining(i) as typeof i)
                );
                expect(comp.<%= otherEntity.entityInstancePlural %>SharedCollection()).toEqual(expectedCollection);
        });

  <%_ } _%>
  <%_ for (const relationship of relationshipsWithCustomUniqueOptions) { %>
    <%_ const otherEntity = relationship.otherEntity _%>
        it('should call <%= relationship.propertyName %> query and add missing value', () => {
                const <%= entityInstance %> : I<%= entityAngularName %> = <%- testEntityPrimaryKey1 %>;
                const <%= relationship.propertyName %> : I<%= otherEntity.entityAngularName %> = <%- otherEntity.tsPrimaryKeySamples[0] %>;
                <%= entityInstance %>.<%= relationship.propertyName %> = <%= relationship.propertyName %>;

                const <%= relationship.propertyName %>Collection: I<%= otherEntity.entityAngularName %>[] = [<%- otherEntity.tsPrimaryKeySamples[0] %>];
                <%= clientTestFramework %>.spyOn(<%= otherEntity.entityInstance %>Service, 'query').mockReturnValue(of(new HttpResponse({ body: <%= relationship.propertyName %>Collection })));
                const expectedCollection: I<%= otherEntity.entityAngularName %>[] = [<%= relationship.propertyName %>, ...<%= relationship.propertyName %>Collection];
                <%= clientTestFramework %>.spyOn(<%= otherEntity.entityInstance %>Service, 'add<%= otherEntity.entityAngularName %>ToCollectionIfMissing').mockReturnValue(expectedCollection);

                activatedRoute.data = of({ <%= entityInstance %> });
                comp.ngOnInit();

                expect(<%= otherEntity.entityInstance %>Service.query).toHaveBeenCalled();
                expect(<%= otherEntity.entityInstance %>Service.add<%= otherEntity.entityAngularName %>ToCollectionIfMissing).toHaveBeenCalledWith(<%= relationship.propertyName %>Collection, <%= relationship.propertyName %>);
                expect(comp.<%= relationship.relationshipFieldNamePlural %>Collection()).toEqual(expectedCollection);
        });

  <%_ } _%>
<%_ } _%>
        it('should update editForm', () => {
            const <%= entityInstance %>: I<%= entityAngularName %> = <%- testEntityPrimaryKey1 %>;
<%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions) { _%>
  <%_ for (const relationship of relationshipsByEntityNeedingOptions) { _%>
    <%_ const otherEntity = relationship.otherEntity _%>
            const <%= relationship.relationshipName %>: I<%= otherEntity.entityAngularName %> = <%- otherEntity.tsPrimaryKeySamples[0] %>;
            <%= entityInstance %>.<%= relationship.propertyName %> = <%= relationship.collection ? `[${relationship.relationshipName}]` : relationship.relationshipName %>;
  <%_ } _%>
<%_ } _%>

            activatedRoute.data = of({ <%= entityInstance %> });
            comp.ngOnInit();

<%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions) { _%>
  <%_ const relationshipsWithCustomUniqueOptions = relationshipsByEntityNeedingOptions.filter(rel => rel.relationshipOneToOne && rel.otherRelationship); _%>
  <%_ for (const relationship of relationshipsByEntityNeedingOptions) { _%>
    <%_ const otherEntity = relationship.otherEntity _%>
            expect(comp.<% if (!relationshipsWithCustomUniqueOptions.includes(relationship)) { %><%= otherEntity.entityInstancePlural %>Shared<% } else { %><%= relationship.relationshipFieldNamePlural %><% } %>Collection()).toContainEqual(<%= relationship.relationshipName %>);
  <%_ } _%>
<%_ } _%>
            expect(comp.<%= entityInstance %>).toEqual(<%= entityInstance %>);
        });
    });

    describe('save', () => {
<%_ if (updatableEntity) { _%>
        it('should call update service on save for existing entity', () => {
            // GIVEN
            const saveSubject = new Subject<HttpResponse<I<%= entityAngularName %>>>();
            const <%= entityInstance %> = <%- testEntityPrimaryKey0 %>;
            <%= clientTestFramework %>.spyOn(<%= entityInstance %>FormService, 'get<%= entityAngularName %>').mockReturnValue(<%= entityInstance %>);
            <%= clientTestFramework %>.spyOn(<%= entityInstance %>Service, 'update').mockReturnValue(saveSubject);
            <%= clientTestFramework %>.spyOn(comp, 'previousState');
            activatedRoute.data = of({ <%= entityInstance %> });
            comp.ngOnInit();

            // WHEN
            comp.save();
            expect(comp.isSaving).toEqual(true);
            saveSubject.next(new HttpResponse({ body: <%= entityInstance %> }));
            saveSubject.complete();

            // THEN
            expect(<%= entityInstance %>FormService.get<%= entityAngularName %>).toHaveBeenCalled();
            expect(comp.previousState).toHaveBeenCalled();
            expect(<%= entityInstance %>Service.update).toHaveBeenCalledWith(expect.objectContaining(<%= entityInstance %>));
            expect(comp.isSaving).toEqual(false);
        });

<%_ } _%>
        it('should call create service on save for new entity', () => {
            // GIVEN
            const saveSubject = new Subject<HttpResponse<I<%= entityAngularName %>>>();
            const <%= entityInstance %> = <%- testEntityPrimaryKey0 %>;
            <%= clientTestFramework %>.spyOn(<%= entityInstance %>FormService, 'get<%= entityAngularName %>').mockReturnValue({ <%= primaryKey.name %>: null });
            <%= clientTestFramework %>.spyOn(<%= entityInstance %>Service, 'create').mockReturnValue(saveSubject);
            <%= clientTestFramework %>.spyOn(comp, 'previousState');
            activatedRoute.data = of({ <%= entityInstance %>: null });
            comp.ngOnInit();

            // WHEN
            comp.save();
            expect(comp.isSaving).toEqual(true);
            saveSubject.next(new HttpResponse({ body: <%= entityInstance %> }));
            saveSubject.complete();

            // THEN
            expect(<%= entityInstance %>FormService.get<%= entityAngularName %>).toHaveBeenCalled();
            expect(<%= entityInstance %>Service.create).toHaveBeenCalled();
            expect(comp.isSaving).toEqual(false);
            expect(comp.previousState).toHaveBeenCalled();
        });
<%_ if (updatableEntity) { _%>

        it('should set isSaving to false on error', () => {
            // GIVEN
            const saveSubject = new Subject<HttpResponse<I<%= entityAngularName %>>>();
            const <%= entityInstance %> = <%- testEntityPrimaryKey0 %>;
            <%= clientTestFramework %>.spyOn(<%= entityInstance %>Service, 'update').mockReturnValue(saveSubject);
            <%= clientTestFramework %>.spyOn(comp, 'previousState');
            activatedRoute.data = of({ <%= entityInstance %> });
            comp.ngOnInit();

            // WHEN
            comp.save();
            expect(comp.isSaving).toEqual(true);
            saveSubject.error('This is an error!');

            // THEN
            expect(<%= entityInstance %>Service.update).toHaveBeenCalled();
            expect(comp.isSaving).toEqual(false);
            expect(comp.previousState).not.toHaveBeenCalled();
        });
<%_ } _%>
    });

<%_ const trackedRelationships = Object.values(relationshipsByOtherEntity).filter(arr => arr.some(rel => rel.persistableRelationship && !rel.otherEntity.embedded));
    if (trackedRelationships.length > 0) {
_%>

    describe('Compare relationships', () => {
  <%_ for (const relationship of trackedRelationships) {
        const { otherEntity } = relationship[0];
  _%>
        describe('compare<%= otherEntity.entityAngularName %>', () => {
            it('should forward to <%= otherEntity.entityInstance %>Service', () => {
                const entity = <%- otherEntity.tsPrimaryKeySamples[0] %>;
                const entity2 = <%- otherEntity.tsPrimaryKeySamples[1] %>;
                <%= clientTestFramework %>.spyOn(<%= otherEntity.entityInstance %>Service, 'compare<%= otherEntity.entityAngularName %>');
                comp.compare<%= otherEntity.entityAngularName %>(entity, entity2);
                expect(<%= otherEntity.entityInstance %>Service.compare<%= otherEntity.entityAngularName %>).toHaveBeenCalledWith(entity, entity2);
            });
        });

  <%_ } _%>
    });
<%_ } _%>
});
